

var publicObj = {
    init: function () {
        String.prototype.replaceAll = function (s1, s2) {
            return this.replace(new RegExp(s1, "gm"), s2);
        };

        this.makeLoadDom();
        this.makeAlertDom();
    },
    getIosAndData:function () {
        var sUserId = sessionStorage.getItem("sUserId");
        if(sUserId == null){
            sessionStorage.clear();
            sessionStorage.setItem('sCorpId', publicObj.getUrlData("sCorpId"));
            sessionStorage.setItem('sStoreId', publicObj.getUrlData("sStoreId"));
            sessionStorage.setItem('sUserId', publicObj.getUrlData("sUserId"));
            sessionStorage.setItem('token', publicObj.getUrlData("token"));
        }
    },
    makeLoadDom:function () {
        var loadStr = '  <div class="loadbox">\n' +
            '        <div class="spinner">\n' +
            '            <div class="rect1"></div>\n' +
            '            <div class="rect2"></div>\n' +
            '            <div class="rect3"></div>\n' +
            '            <div class="rect4"></div>\n' +
            '            <div class="rect5"></div>\n' +
            '        </div>\n' +
            '    </div>'  ;
        $("body").append(loadStr);
    },
    showLoad:function () {
        $(".loadbox").show();
    },
    hideLoad:function () {
        $(".loadbox").hide();
    },
    makeAlertDom: function () {
        var domStr = '<div class="myalert" >\n' +
            '        <div class="layer"></div>\n' +
            '        <div class="tip"></div>\n' +
            '    </div>'
        $("body").append(domStr);
    },
    myalert: function (tip) {
        $(".myalert > .tip").text(tip);
        $(".myalert").show()
        var int;
        clearTimeout(int)
        int = setTimeout(function () {
            $(".myalert").hide();
            $(".myalert > .tip").text('');
        }, 800);

    },
    getUrlData: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    },
    myAjax: function (obj, fun, reqUrl, reqBody, reqType, isAsync, dataType,contentType,callbackparameter) {
        var obj = obj;
        var dataType = dataType;
        var dataObj = reqBody;
        var callbackparameter = callbackparameter;
        dataObj.sCorpId = sessionStorage.getItem("sCorpId");
        dataObj.sStoreId = sessionStorage.getItem("sStoreId");
        dataObj.sUserId = sessionStorage.getItem("sUserId");
        dataObj.token = sessionStorage.getItem("token");
        //签名加密
        var asciiDataObj = this.sort_ASCII(dataObj);
        var signStr = '';
        for (var key in asciiDataObj) {
            var valueStr = asciiDataObj[key];
            if (typeof asciiDataObj[key] == "object") {
                valueStr = JSON.stringify(asciiDataObj[key])
            }
            if (asciiDataObj[key] != "") {
                signStr += key + '=' + valueStr + '&';
            }
        }
        var sSgin = signStr + 'key=ruepay_1107';
        dataObj.sign = md5(sSgin).toUpperCase();
        var oContentType = "application/x-www-form-urlencoded";
        if(contentType == undefined|| contentType == "application/x-www-form-urlencoded"){
            contentType = oContentType;
        }else{
            dataObj = JSON.stringify(dataObj);
        }
        $.ajax({
            url: reqUrl,
            type: reqType,
            data: dataObj,
            async: isAsync,
            dataType: dataType,
            contentType: contentType,
            success: function (res) {
                dataObj = {};
                if (dataType == 'text') {
                    var resObj = JSON.parse(res)
                    fun(resObj, obj,callbackparameter)
                } else {
                    fun(res, obj,callbackparameter)
                }

            },
            error: function (res) {
                fun(res)
                dataObj = {};
                publicObj.hideLoad();
            }
        });
    },
    sort_ASCII: function (obj) {
        var arr = new Array();
        var num = 0;
        for (var i in obj) {
            arr[num] = i;
            num++;
        }
        var sortArr = arr.sort();
        var sortObj = {};
        for (var i in sortArr) {
            sortObj[sortArr[i]] = obj[sortArr[i]];
        }
        return sortObj;
    },
    plusNum:function (arg1, arg2) {
        var r1, r2, m, c;
        try {
            r1 = arg1.toString().split(".")[1].length;
        }
        catch (e) {
            r1 = 0;
        }
        try {
            r2 = arg2.toString().split(".")[1].length;
        }
        catch (e) {
            r2 = 0;
        }
        c = Math.abs(r1 - r2);
        m = Math.pow(10, Math.max(r1, r2));
        if (c > 0) {
            var cm = Math.pow(10, c);
            if (r1 > r2) {
                arg1 = Number(arg1.toString().replace(".", ""));
                arg2 = Number(arg2.toString().replace(".", "")) * cm;
            } else {
                arg1 = Number(arg1.toString().replace(".", "")) * cm;
                arg2 = Number(arg2.toString().replace(".", ""));
            }
        } else {
            arg1 = Number(arg1.toString().replace(".", ""));
            arg2 = Number(arg2.toString().replace(".", ""));
        }
        return (arg1 + arg2) / m;
    },
    reduceNum:function (arg1 , arg2) {
        var r1, r2, m, n;
        try {
            r1 = arg1.toString().split(".")[1].length;
        }
        catch (e) {
            r1 = 0;
        }
        try {
            r2 = arg2.toString().split(".")[1].length;
        }
        catch (e) {
            r2 = 0;
        }
        m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度
        n = (r1 >= r2) ? r1 : r2;
        return Number(((arg1 * m - arg2 * m) / m).toFixed(n));
    },
    accMul: function (arg1, arg2) {
        var m = 0, s1 = arg1.toString(),
            s2 = arg2.toString();
        try {
            m += s1.split(".")[1].length
        } catch (e) {
        }
        try {
            m += s2.split(".")[1].length
        } catch (e) {
        }
        return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
    },
    except: function (arg1, arg2) {
        var t1 = 0, t2 = 0, r1, r2,m,n;
        try {
            t1 = arg1.toString().split(".")[1].length;
        }
        catch (e) {
        }
        try {
            t2 = arg2.toString().split(".")[1].length;
        }
        catch (e) {
        }
        with (Math) {
            r1 = Number(arg1.toString().replace(".", ""));
            r2 = Number(arg2.toString().replace(".", ""));
            return this.accMul((r1 / r2),pow(10, t2 - t1));
        }
    },

    };
publicObj.init();
